package com.locationtracker.plus.data.repository

import com.locationtracker.plus.app.network.apiService
import com.locationtracker.plus.app.util.CacheUtil
import com.locationtracker.plus.data.model.bean.ApiResponse
import com.locationtracker.plus.data.model.bean.UserInfo
import me.hgj.jetpackmvvm.network.AppException

val HttpRequestCoroutine: HttpRequestManger by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    HttpRequestManger()
}

class HttpRequestManger {

    suspend fun loginByCode(phone: String, code: String): ApiResponse<UserInfo> {
        val registerData = apiService.login(phone, code)
        if (registerData.isSucces()) {
            CacheUtil.setToken(registerData.getResponseData().token)
            if (CacheUtil.getForeverId().isNotEmpty()) apiService.updatePayEvent(CacheUtil.getForeverId())
            return apiService.getUserInfo()
        } else {
            //抛出错误异常
            throw AppException(registerData.getResponseCode(), registerData.getResponseMsg())
        }
    }
}